package ar.pmt.gwt.client.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.gwtext.client.core.UrlParam;

import ar.pmt.vista.beans.VistaBean;

public class MethodRequest {
	
	private String clase;
	
	private String metodo;
	
	private String templateOut;
	
	private  Collection<Class<? extends VistaBean >> clasesArg;
	
	private Map<String, String> atributosExtras;
	
	
	public MethodRequest(String clase, String metodo, Set<Class<? extends VistaBean >> clasesArg) {
		this.clase = clase;
		this.metodo = metodo;
		this.clasesArg = clasesArg;
		this.atributosExtras = new HashMap<String, String>();
		this.setTemplateOut(ConstantesGwt.TEMPLATE_DEFAULT);
	}
	
	
	public MethodRequest(String clase, String metodo) {
		this.clase = clase;
		this.metodo = metodo;
		this.clasesArg = new ArrayList<Class<? extends VistaBean >>();
		this.atributosExtras = new HashMap<String, String>();
		this.setTemplateOut(ConstantesGwt.TEMPLATE_DEFAULT);
	}
	
	public void agregarClasesArgumento(Class<? extends VistaBean > claseArg){
		this.clasesArg.add(claseArg);
	}
	
	public void agregarAtributos(String atributo, String valor){
		this.atributosExtras.put(atributo, valor);
	}

	/**
	 * @return the clase
	 */
	public final String getClase() {
		return clase;
	}


	/**
	 * @return the metodo
	 */
	public final String getMetodo() {
		return metodo;
	}


	/**
	 * @return the clasesArg
	 */
	public final Collection<Class<? extends VistaBean >> getClasesArg() {
		return clasesArg;
	}


	/**
	 * @return the atributosExtras
	 */
	public final Map<String, String> getAtributosExtras() {
		return atributosExtras;
	}


	/**
	 * @param metodo the metodo to set
	 */
	public final void setMetodo(String metodo) {
		this.metodo = metodo;
	}
	
	
	/**
	 * @param templateOut the templateOut to set
	 */
	public final void setTemplateOut(String templateOut) {
		this.templateOut = templateOut;
	}


	/**
	 * @return the templateOut
	 */
	public final String getTemplateOut() {
		return templateOut;
	}


	public String toRequestUrl(){
	
		String clase = "_clase="+this.getClase();
		String metodo = "_metodo="+this.getMetodo();
		String template = "_template="+this.templateOut;
		
		Iterator<Class<? extends VistaBean >> itClase = this.getClasesArg().iterator();
		String claseArgumento ="";
		while (itClase.hasNext()) {
			Class<? extends VistaBean > claseArg = (Class<? extends VistaBean >) itClase.next();
			claseArgumento = claseArgumento+"_clasesArg="+claseArg.getName()+"&";
		}
		int ind = claseArgumento.lastIndexOf('&');
		if(ind!=-1){
			claseArgumento = claseArgumento.substring(0,ind);
		}
		String atributos = "";
		Iterator<Entry<String, String>> itAtributosExtras = this.getAtributosExtras().entrySet().iterator();
		while (itAtributosExtras.hasNext()) {
		        Map.Entry<String, String> pairs = (Map.Entry<String, String>)itAtributosExtras.next();
		        atributos = atributos +pairs.getKey()+"="+pairs.getValue()+"&";
		}
		int indA = atributos.lastIndexOf('&');
		if(indA!=-1){
			atributos = atributos.substring(0,indA);
		}
		String request = "?"+clase+"&"+metodo+"&"+template;
		if(!claseArgumento.equals("")){
			request = request+"&"+claseArgumento;
		}
		if(!atributos.equals("")){
			request = request+"&"+atributos;
		}		
		return request;
	}
	
	
	
	

}
